<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          v-hasPermi="['purchase:productStatistical:query']"
          @click="handleSearch"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport()"
          v-hasPermi="['purchase:productStatistical:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="审核日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              :clearable="false"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购单号" prop="cgdh">
            <el-input
              v-model="queryParams.cgdh"
              placeholder="请输入采购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="wldm">
            <el-input
              v-model="queryParams.wldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="厂商代码">
            <el-select v-model="queryParams.params.csdm" clearable size="small">
              <el-option
                v-for="dict in csmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
            -
            <el-select
              v-model="queryParams.params.csdmEnd"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in csmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="综合查询" prop="pmgg">
            <el-input
              v-model="queryParams.pmgg"
              style="width: 500px"
              type="textarea"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-radio-group v-model="queryParams.pfmZtbz">
              <el-radio label="1" style="margin-left: 150px">未入库</el-radio>
              <el-radio label="2">已入库</el-radio>
              <el-radio label="0">全部</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 子表TAB-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          id="tabMx"
          highlight-current-row
          v-loading="loading"
          :data="detailList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="采购单号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdDjbh"
          />
          <el-table-column
            label="厂商"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="csmCsmc"
          />
          <el-table-column
            label="采购预期"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfmJhrq"
            width="180"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.pfmJhrq, "{y}-{m}-{d} ") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="请购单号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdQfdh"
          />
          <el-table-column
            label="请购部门"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="pfdBmdm"
          >
            <template v-slot="scope">
              <dict-tag :options="deptOptions" :value="scope.row.pfdBmdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdCgsl"
          />
          <el-table-column
            label="采购单价"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdCgdj"
          />
          <el-table-column
            label="采购金额"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdCgje"
          />
          <el-table-column
            label="采购状态"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="lqmZtbzmc"
          />
          <el-table-column
            label="备注"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdDesc"
          />
          <el-table-column
            label="变更原因"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdBgdesc"
          />
          <el-table-column
            label="请购人员"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="lqmJlrymc"
          />
          <el-table-column
            label="请购日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="lqmJlrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.lqmJlrq, "{y}-{m}-{d} {hh}:{mm}:{ss}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="开单人员"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdJlrymc"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdJlrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.pfdJlrq, "{y}-{m}-{d} {hh}:{mm}:{ss}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="变更人员"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfdBgrymc"
          />
          <el-table-column
            label="变更日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfdBgrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.pfdBgrq, "{y}-{m}-{d} {hh}:{mm}:{ss}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="pfmShrymc"
          />
          <el-table-column
            label="审核日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="pfmShrq"
            width="180"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.pfmShrq, "{y}-{m}-{d} {hh}:{mm}:{ss}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按部门统计" name="second">
        <el-table
          id="tabBmtj"
          highlight-current-row
          v-loading="loading"
          :data="deptList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="请购部门"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="pxBmmc"
          >
            <template v-slot="scope">
              <dict-tag :options="deptOptions" :value="scope.row.pxBmmc" />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="vwldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="vpmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="vdw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.vdw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="vcgsl"
          />
          <el-table-column
            label="采购金额"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="vcgje"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按厂商统计" name="third">
        <el-table
          id="tabCstj"
          highlight-current-row
          v-loading="loading"
          :data="csdmList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load3"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="厂商"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="pxCsmc"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="vwldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="vpmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="vdw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.vdw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购数量"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="vcgsl"
          />
          <el-table-column
            label="采购金额"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="vcgje"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getAllCsm } from "@/api/purchase/csm";
import { exportTotalList, pfmReportList } from "@/api/purchase/pfmMstr";
import { listDept } from "@/api/system/dept";
import { exportTable } from "@/utils/ruoyi";
import { exportClmList } from "@/api/output/clmReport";

export default {
  name: "PfmReport",
  data() {
    return {
      activeTab: "first",
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 240,
      // 导出遮罩层
      exportLoading: false,
      dateRange: [
        this.$moment().startOf("month").toDate(),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 60),
      ],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      itmDwOptions: [],
      deptOptions: [],
      csmOptions: [],
      //采购明细总数 和 详细表
      detailTotal: 0,
      detailList: [],
      //部门统计 总数 和 List
      deptTotal: 0,
      deptList: [],
      //厂商统计 总数 和 List
      csdmTotal: 0,
      csdmList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        cgdh: null,
        wldm: null,
        pmgg: null,
        pfmZtbz: "1",
        params: {
          shrq: null,
          shrqEnd: null,
          csdm: "HD000",
          csdmEnd: "HD999",
        },
        excelIndex: 1,
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });
    getAllCsm().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.csmCsdm + " " + i.csmCsmc;
        i.dictValue = i.csmCsdm + "";
        i.listClass = "default";
      }
      this.csmOptions = response.data;
      this.queryParams.csdm = this.csmOptions[0].csmCsdm;
    });
    // 获取全部部门
    listDept().then((res) => {
      this.deptOptions = res.data;
      for (let item of this.deptOptions) {
        item.dictLabel = item.deptName;
        item.listClass = "default";
        item.dictValue = item.deptId + "";
      }
    });
  },
  methods: {
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询待生产采购明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.params.shrq = null;
      this.queryParams.params.shrqEnd = null;
      if (this.dateRange !== null) {
        this.queryParams.params.shrq = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.params.shrqEnd = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      return pfmReportList(this.queryParams).then((response) => {
        // 明细数据
        this.detailList = this.detailList.concat(response.data.list1.rows);
        this.detailTotal = response.data.list1.total;
        // 按部门统计
        this.deptList = this.deptList.concat(response.data.list2.rows);
        this.deptTotal = response.data.list2.total;
        // 按厂商统计
        this.csdmList = this.csdmList.concat(response.data.list3.rows);
        this.csdmTotal = response.data.list3.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.detailTotal / this.queryParams.pageSize)
      ) {
        // this.queryParams.pageSize = 20
        this.queryParams.pageNum++;
        this.getList();
      }

      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.deptTotal / this.queryParams.pageSize)
      ) {
        // this.queryParams.pageSize = 20
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.csdmTotal / this.queryParams.pageSize)
      ) {
        // this.queryParams.pageSize = 20
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.deptList = [];
      this.detailList = [];
      this.csdmList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProductQgdhTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSearch() {
      this.title = "查询零星采购统计分析";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有零星采购统计分析?", "警告", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportTotalList(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
